package com.lam.common.components.data.model;

import java.lang.reflect.Method;
import java.util.List;

import com.lam.common.components.data.annotation.DataInject;
import com.lam.common.utils.StringUtils;
import com.lam.common.utils.sql.SqlUtil;

public class DataInjectConfig {
	 
	public static final String EXPIRE_AUTO = "AUTO";
	
	/** 字段名 */
	private String fieldName;
	
	/** 转换字典时源值的字段名，不填默认为当前字段 */
	private String origField;
	
    /** 查询数据的表名 */
	private String tableName;
	
	/** 数据的表名字段 */
	private List<String> columnList;
	private List<String> origFieldList;
	private List<Method> origGetterList;
	
    /** 查询tableName表的数据库字段名称，默认为全部* */
	private String listField;
    
    /** 查询SQL返回的结果类型 */
	private ResultType resultType;
	
	private Class<?> dataType;
	
	/** 是否缓存 */
	private boolean useCache;
    
    /**
     * 查询结果的缓存有效期，为空则不缓存，默认: conditionSql 2小时, customSql 5分钟
     * <br>例：
     * <pre>
     *  1天：1d或1D
     *  2小时：2h或2H 
     *  10分钟：10m或10M
     *  30秒：30s或30S
     * </pre>
     */
	private String expire;
    
    /** 查询条件SQL，支持变量，变量用{变量名称}占位 */
	private String conditionSql;
    
    /** 自定义SQL，优先使用此属性，支持变量，变量用{变量名称}占位 */
	private String customSql;
	
	private List<String> varList;
	
	private List<Method> varFieldGetterList;
	
	private Method targetSetter;
	
	public DataInjectConfig(String fieldName, DataInject dataInject) {
		this.fieldName = fieldName;
		this.origField = dataInject.origField();
		
		this.tableName = dataInject.tableName();
		this.listField = StringUtils.isBlank(dataInject.listField())?"*":dataInject.listField();
		this.resultType = dataInject.resultType();
		this.dataType = dataInject.dataType();
		this.useCache = dataInject.useCache();
		this.expire = dataInject.expire();
		
		this.conditionSql = dataInject.conditionSql();
		this.customSql = dataInject.customSql();
		if(StringUtils.isNotBlank(customSql)) {
			this.varList = SqlUtil.getVarList(customSql);
			this.tableName = SqlUtil.getTableNameBySql(customSql).toString();
		}else {
			this.varList = SqlUtil.getVarList(conditionSql);
		}
	}
	
	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getListField() {
		return listField;
	}

	public void setListField(String listField) {
		this.listField = listField;
	}

	public ResultType getResultType() {
		return resultType;
	}

	public void setResultType(ResultType resultType) {
		this.resultType = resultType;
	}

	public String getExpire() {
		return expire;
	}

	public void setExpire(String expire) {
		this.expire = expire;
	}

	public String getConditionSql() {
		return conditionSql;
	}

	public void setConditionSql(String conditionSql) {
		this.conditionSql = conditionSql;
	}

	public String getCustomSql() {
		return customSql;
	}

	public void setCustomSql(String customSql) {
		this.customSql = customSql;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public List<String> getVarList() {
		return varList;
	}

	public void setVarList(List<String> varList) {
		this.varList = varList;
	}

	public List<Method> getVarFieldGetterList() {
		return varFieldGetterList;
	}

	public void setVarFieldGetterList(List<Method> varFieldGetterList) {
		this.varFieldGetterList = varFieldGetterList;
	}

	public String getOrigField() {
		return origField;
	}

	public void setOrigField(String origField) {
		this.origField = origField;
	}

	public Method targetSetter() {
		return targetSetter;
	}

	public void setTargetSetter(Method targetSetter) {
		this.targetSetter = targetSetter;
	}

	public Class<?> getDataType() {
		return dataType;
	}

	public void setDataType(Class<?> dataType) {
		this.dataType = dataType;
	}

	public List<String> getOrigFieldList() {
		return origFieldList;
	}

	public void setOrigFieldList(List<String> origFieldList) {
		this.origFieldList = origFieldList;
	}

	public List<Method> origGetterList() {
		return origGetterList;
	}

	public void setOrigGetterList(List<Method> origGetterList) {
		this.origGetterList = origGetterList;
	}

	public List<String> columnList() {
		return columnList;
	}

	public void setColumnList(List<String> columnList) {
		this.columnList = columnList;
	}

	public boolean isUseCache() {
		return useCache;
	}

	public void setUseCache(boolean useCache) {
		this.useCache = useCache;
	}

}
